package deployr.cli;

import deployr.core.*;
import deployr.domain.*;

public class UpdateUpdateDiffResponder extends Responder {

	private final Workspace workspace;

	public UpdateUpdateDiffResponder(Workspace workspace) {
		this.workspace = workspace;
	}
	
	@Override
	protected boolean shouldRespond(String method, String requestURI) {
		return "POST".equals(method) && requestURI.matches(".*/updates/.*/diff");
	}
	
	@Override
	protected Result respondTo(String method, String requestURI, Input input) {
		String diff = ((String[]) input.getParameters().get("diff"))[0];
		
		String updateKey = requestURI.replaceAll(".*/updates/", "").replaceAll("/diff.*", "");

		IUpdate update = workspace.findUpdate(updateKey);
		if(notFound(update)) return new Result(404);
		update.updateDiff(diff);
		
		workspace.store(update);
		
		return new Result(200);
	}

}